<template>
  <div class="visit-type-container">
    <div class="tabs-grid">
      <div
        v-for="(tab, idx) in tabs"
        :key="tab.value"
        class="tab-card"
        :class="{ active: modelValue === idx }"
        @click="handleSelect(idx)"
      >
        <div v-if="modelValue === idx" class="corner-badge">
          <div class="checkmark"></div>
        </div>
        <h4 class="card-title">{{ tab.name }}</h4>
        <p class="card-desc">{{ tab.subTitle }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineEmits, defineExpose } from "vue";
import { CommunicationTargetType } from "../type";

interface TabItem {
  name: string;
  subTitle: string;
  type: CommunicationTargetType;
}

const props = defineProps<{
  modelValue?: number;
}>();

const emit = defineEmits<{
  "update:modelValue": [value: number];
  change: [tab: TabItem, index: number];
}>();

const selectedIndex = ref(props.modelValue || 0);

const tabs = ref<TabItem[]>([
  {
    name: "商机",
    subTitle: "在商机下安排沟通",
    type: CommunicationTargetType.Opportunity,
  },
  {
    name: "客户",
    subTitle: "在客户下安排沟通",
    type: CommunicationTargetType.Customer,
  },
]);

const handleSelect = (index: number) => {
  const tabItem: TabItem = tabs.value[index];
  emit("update:modelValue", tabItem.type);
  emit("change", tabItem, index);
};
</script>
<style scoped lang="less">
.visit-type-container {
  padding: 16px;
  background: #ffffff;
}

.tabs-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.tab-card {
  position: relative;
  background: #ffffff;
  border: 1px solid #e5e6eb;
  border-radius: 8px;
  padding: 16px;
  min-height: 72px;
  cursor: pointer;
  transition: all 0.2s ease;
  box-sizing: border-box;

  &:hover {
    border-color: #d1d5db;
  }

  &.active {
    border-color: #436bff;
    background: #ffffff;
  }
}

.card-title {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 22px;
  color: #262626;
}

.card-desc {
  margin: 0;
  font-size: 12px;
  line-height: 18px;
  color: #8c8c8c;
}

/* 右上角蓝色三角角标 */
.corner-badge {
  position: absolute;
  top: -1px;
  right: -1px;
  width: 24px;
  height: 20px;
  background: #436bff;
  clip-path: polygon(100% 0, 0 0, 100% 100%);
  border-top-right-radius: 8px;
  z-index: 1;
}

/* 角标内的白色对勾 */
.checkmark {
  position: absolute;
  top: 4px;
  right: 5px;
  width: 6px;
  height: 3px;
  border-left: 1.5px solid #ffffff;
  border-bottom: 1.5px solid #ffffff;
  transform: rotate(-45deg);
}
</style>
